using System.Collections;
using UnityEngine;

public class ActivateCar : MonoBehaviour
{
	private GameObject objPlayer;

	private float distDeactivate = 150f;

	private bool isActive = true;

	private NavMeshObstacle _navMeshObstacle;

	private Renderer[] arrRenderObj;

	private Animation[] arrAnimation;

	private EntityBehavior entityBehavior;

	private void Start()
	{
		arrRenderObj = GetComponentsInChildren<Renderer>(true);
		arrAnimation = GetComponentsInChildren<Animation>(true);
		entityBehavior = GetComponent<EntityBehavior>();
		if (GameController.instance.curTypeGame != TypeGame.RocketLeague)
		{
			StartCoroutine(Check());
		}
	}

	private IEnumerator Check()
	{
		while (true)
		{
			if (objPlayer == null && GameController.instance != null)
			{
				objPlayer = GameController.instance.myPlayer;
			}
			if (objPlayer != null)
			{
				if (Vector3.Distance(objPlayer.transform.position, base.transform.position) > distDeactivate)
				{
					if (isActive)
					{
						yield return StartCoroutine(ActivCar(false));
					}
				}
				else if (!isActive)
				{
					yield return StartCoroutine(ActivCar(true));
				}
			}
			yield return new WaitForSeconds(0.5f);
		}
	}

	private IEnumerator ActivCar(bool val)
	{
		for (int j = 0; j < arrRenderObj.Length; j++)
		{
			if (arrRenderObj[j] != null)
			{
				arrRenderObj[j].enabled = val;
			}
		}
		for (int i = 0; i < arrAnimation.Length; i++)
		{
			if (arrAnimation[i] != null)
			{
				arrAnimation[i].enabled = val;
			}
		}
		entityBehavior.carIndicatorHolder.gameObject.SetActive(val);
		isActive = val;
		if (val && !entityBehavior.isDead)
		{
			entityBehavior.SwitchTexture(entityBehavior.GetCurrentSkin(), true);
		}
		if (Device.indexWeakDevice == 0 && entityBehavior.privateCar && !entityBehavior.carWithoutShadow && PlayerBehavior.MyPlayer.isMine)
		{
			entityBehavior.bcwShadowController.TurnOnEntityShadow(val);
		}
		yield return null;
	}
}
